package com.qjc.demo.algorithm.字符串中的重叠词;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 输出字符串中所有的叠词（几个连续出现的字组成叠词）
 * 输入： '晴川历历汉阳树，芳草萋萋鹦鹉洲'
 * 输出： [ '历历', '萋萋' ]
 * @Author: qjc
 * @Date: 2020/3/18
 */
public class ChongDieCi {


    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        String wordString = "晴川历历汉历历历历阳历历树，芳草萋萋萋鹦鹉洲";

        String chongDieWord = "";
        char preWord = wordString.charAt(0);
        for (int i = 1; i < wordString.length(); ) {
            chongDieWord = chongDieWord.concat(String.valueOf(preWord));
            char afterWord = wordString.charAt(i);
            if (preWord == afterWord) {
                i++;
                if (i == wordString.length()) {
                    chongDieWord = chongDieWord.concat(String.valueOf(preWord));
                    if (chongDieWord.length() > 1) {
                        list.add(chongDieWord);
                    }
                }
            } else {
                if (chongDieWord.length() > 1) {
                    list.add(chongDieWord);
                }
                chongDieWord = "";
                preWord = afterWord;
                i++;
            }
        }

//        char[] chars = wordString.toCharArray();
//        for (int i = 0; i < chars.length - 1; i++) {
//            boolean flag = false;
//            String chongDieWord = String.valueOf(chars[i]);
//            for (int j = i + 1; j < chars.length; j++) {
//                String s = String.valueOf(chars[j]);
//                String substring = chongDieWord.substring(0, 1);
//                if (substring.equals(s)) {
//                    chongDieWord = chongDieWord + s;
//                    flag = true;
//                    i++;
//                } else {
//                    break;
//                }
//            }
//            if (flag) {
//                list.add(chongDieWord);
//            }
//        }
        for (String s : list) {
            System.err.println(s);
        }
    }

}
